<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Wxc\Mapper;

use App\Wxc\Model\WxcSharerForm;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 享员申请单Mapper类
 */
class WxcSharerFormMapper extends AbstractMapper
{
    /**
     * @var WxcSharerForm
     */
    public $model;

    public function assignModel()
    {
        $this->model = WxcSharerForm::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 申请单ID
        if (isset($params['form_id']) && filled($params['form_id'])) {
            $query->where('form_id', '=', $params['form_id']);
        }

        // 状态。0：待处理；200：审核通过；255：审核不通过
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 原因
        if (isset($params['reason']) && filled($params['reason'])) {
            $query->where('reason', 'like', '%'.$params['reason'].'%');
        }

        // 
        if (isset($params['site_id']) && filled($params['site_id'])) {
            $query->where('site_id', '=', $params['site_id']);
        }

        // 视频号小店appid
        if (isset($params['app_id']) && filled($params['app_id'])) {
            $query->where('app_id', '=', $params['app_id']);
        }

        // 分享员助手小程序用户openid
        if (isset($params['openid']) && filled($params['openid'])) {
            $query->where('openid', '=', $params['openid']);
        }

        // 申请人手机号
        if (isset($params['mobile_number']) && filled($params['mobile_number'])) {
            $query->where('mobile_number', '=', $params['mobile_number']);
        }

        // 昵称
        if (isset($params['nickname']) && filled($params['nickname'])) {
            $query->where('nickname', 'like', '%'.$params['nickname'].'%');
        }

        // 头像url
        if (isset($params['avatar']) && filled($params['avatar'])) {
            $query->where('avatar', 'like', '%'.$params['avatar'].'%');
        }

        // 提交时间
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 更新时间
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        return $query;
    }
}